library(foreign)
library(interplot)
library(MASS)
library(countrycode)
library(lfe)
library(spgwr)
library(doBy)
library(plyr)
library(stargazer)
library(DataCombine)
#Set seed
set.seed(123123)

###Import data
setwd("~/My Drive/AfroGrid/")
afro.grid <- read.csv("Viol_Climate_Development_20211228.csv")
summary(afro.grid)

##################################
###Arranging Monthly Level Data###
##################################

#Create log population variable
afro.grid$logpopulation <- log(afro.grid$population+1)
#Aggregate all ACLED nonstate battles into one variable
afro.grid$acled_battle_nonstate <- afro.grid$acled_battle_idmil+afro.grid$acled_battle_polmil+afro.grid$acled_battle_rebel
#Aggregate all ACLED violence against civilians indicators 
afro.grid$acled_viol_tot <- afro.grid$acled_viol_state+afro.grid$acled_viol_rebel+afro.grid$acled_viol_idmil+afro.grid$acled_viol_polmil
#Aggregate all PITF violence against civilians indicators
afro.grid$pitf_tot <- afro.grid$pitf_state_inc+afro.grid$pitf_nonstate_inc+afro.grid$pitf_multiple_inc+afro.grid$pitf_trans_inc
#Create aggregated riot and demonstration indicator for SCAD
#scad_demo, constructed using: 
afro.grid$scad_demo <- afro.grid$scad_org_demo+afro.grid$scad_spont_demo
table(afro.grid$scad_demo)
#Scad riot
afro.grid$scad_riot <- afro.grid$scad_viol_riot+afro.grid$scad_spont_riot
table(afro.grid$scad_riot)
#Create a time trend indicator
t <- 1:length(unique(afro.grid$ym))
g <- summaryBy(month+year~ym, data=afro.grid, keep.names = T)
g <- g[order(g$year, g$month),]
g$ym <- NULL
t2 <- data.frame(cbind(g,t))
summary(t2)
#Aggregate into the data
afro.grid <- join(afro.grid, t2)
summary(afro.grid$t )

#####Create lags of all relevant variables
#Arrange the panel data effectively
afro.grid <- afro.grid[order(afro.grid$gid, afro.grid$t),]
###General lags
##DVs
afro.grid <- slide(afro.grid, Var="ged_state",TimeVar="t", GroupVar="gid", NewVar="lagged_state", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="ged_nonstate",TimeVar="t", GroupVar="gid", NewVar="lagged_nonstate", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="ged_viol_tot",TimeVar="t", GroupVar="gid", NewVar="lagged_viol_tot", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="ged_viol_state",TimeVar="t", GroupVar="gid", NewVar="lagged_viol_state", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="ged_viol_nonstate",TimeVar="t", GroupVar="gid", NewVar="lagged_viol_nonstate", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="acled_battle_state",TimeVar="t", GroupVar="gid", NewVar="lagacled_battle_state", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="acled_battle_nonstate",TimeVar="t", GroupVar="gid", NewVar="lagacled_battle_nonstate", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="acled_battle_rebel",TimeVar="t", GroupVar="gid", NewVar="lagacled_battle_rebel", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="acled_viol_state",TimeVar="t", GroupVar="gid", NewVar="lagacled_viol_state", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="acled_viol_rebel",TimeVar="t", GroupVar="gid", NewVar="lagacled_viol_rebel", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="acled_viol_tot",TimeVar="t", GroupVar="gid", NewVar="lagacled_viol_tot", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="pitf_tot",TimeVar="t", GroupVar="gid", NewVar="lagpitf_tot", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="acled_protests",TimeVar="t", GroupVar="gid", NewVar="lagacled_protests", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="acled_riots",TimeVar="t", GroupVar="gid", NewVar="lagacled_riots", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="scad_demo",TimeVar="t", GroupVar="gid", NewVar="lagscad_demo", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="scad_riot",TimeVar="t", GroupVar="gid", NewVar="lagscad_riot", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)

##IVs and instruments
afro.grid <- slide(afro.grid, Var="NDVI_mean",TimeVar="t", GroupVar="gid", NewVar="lagNDVI_mean", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="logpopulation",TimeVar="t", GroupVar="gid", NewVar="laglogpopulation", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="NL_sum",TimeVar="t", GroupVar="gid", NewVar="lagNL_sum", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="t.c.avg",TimeVar="t", GroupVar="gid", NewVar="lagt.c.avg", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)
afro.grid <- slide(afro.grid, Var="p.avg",TimeVar="t", GroupVar="gid", NewVar="lagp.avg", slideBy = -1,
                   keepInvalid = FALSE, reminder = TRUE)

###Subset data for period of interest
afro.grid.2 <- subset(afro.grid, year>=2003)
summary(afro.grid.2)

#################################
###Arranging Yearly Level Data###
#################################

###Collapse data to the annual level
##Collapse conflict variables
afro.grid.year <- summaryBy(ged_state+ged_nonstate+ged_viol_tot +ged_viol_state+ged_viol_nonstate+acled_battle_state+acled_battle_nonstate+acled_viol_tot+pitf_tot+acled_protests+acled_riots+scad_demo+scad_riot~ gid+year, data=afro.grid, FUN=c("sum"), keep.names = T)
summary(afro.grid.year)
##Collapse average values for IVs
afro.grid.year.mean <- summaryBy(NDVI_mean+logpopulation+NL_sum+t.c.avg+p.avg~ gid+year, data=afro.grid, FUN=c("mean"), keep.names = T)
summary(afro.grid.year.mean)
##Collapse maximum values for IVs
afro.grid.year.max <- summaryBy(NDVI_mean+NL_sum+t.c.avg+p.avg~ gid+year, data=afro.grid, FUN=c("max"), keep.names = T)
summary(afro.grid.year.max)
names(afro.grid.year.max) <- c("gid", "year","NDVI_max","NL_max","t.c.avg_max","p.avg_max")
#Combine all frameworks
afro.grid.year <- join(afro.grid.year,afro.grid.year.mean)
afro.grid.year <- join(afro.grid.year, afro.grid.year.max)
summary(afro.grid.year )

####Create lags of all variables
##Arrange panel data
afro.grid.year <- afro.grid.year[order(afro.grid.year$gid, afro.grid.year$year),]
##Lag DVs
afro.grid.year <- slide(afro.grid.year, Var="ged_state",TimeVar="year", GroupVar="gid", NewVar="lagged_state", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="ged_nonstate",TimeVar="year", GroupVar="gid", NewVar="lagged_nonstate", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="ged_viol_tot",TimeVar="year", GroupVar="gid", NewVar="lagged_viol_tot", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="ged_viol_state",TimeVar="year", GroupVar="gid", NewVar="lagged_viol_state", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="ged_viol_nonstate",TimeVar="year", GroupVar="gid", NewVar="lagged_viol_nonstate", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="acled_battle_state",TimeVar="year", GroupVar="gid", NewVar="lagacled_battle_state", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="acled_battle_nonstate",TimeVar="year", GroupVar="gid", NewVar="lagacled_battle_nonstate", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="acled_viol_tot",TimeVar="year", GroupVar="gid", NewVar="lagacled_viol_tot", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="pitf_tot",TimeVar="year", GroupVar="gid", NewVar="lagpitf_tot", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="acled_protests",TimeVar="year", GroupVar="gid", NewVar="lagacled_protests", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="acled_riots",TimeVar="year", GroupVar="gid", NewVar="lagacled_riots", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="scad_demo",TimeVar="year", GroupVar="gid", NewVar="lagscad_demo", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="scad_riot",TimeVar="year", GroupVar="gid", NewVar="lagscad_riot", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)

##Lag average IVs
afro.grid.year <- slide(afro.grid.year, Var="NDVI_mean",TimeVar="year", GroupVar="gid", NewVar="lagNDVI_mean", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="logpopulation",TimeVar="year", GroupVar="gid", NewVar="laglogpopulation", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="NL_sum",TimeVar="year", GroupVar="gid", NewVar="lagNL_sum", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="t.c.avg",TimeVar="year", GroupVar="gid", NewVar="lagt.c.avg", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="p.avg",TimeVar="year", GroupVar="gid", NewVar="lagp.avg", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
##Lag max value IVs
afro.grid.year <- slide(afro.grid.year, Var="NDVI_max",TimeVar="year", GroupVar="gid", NewVar="lagNDVI_max", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="NL_max",TimeVar="year", GroupVar="gid", NewVar="lagNL_max", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="t.c.avg_max",TimeVar="year", GroupVar="gid", NewVar="lagt.c.avg_max", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)
afro.grid.year <- slide(afro.grid.year, Var="p.avg_max",TimeVar="year", GroupVar="gid", NewVar="lagp.avg_max", slideBy = -1,
                        keepInvalid = FALSE, reminder = TRUE)

##Subset relevant years
afro.grid.year.2 <- subset(afro.grid.year, year>=2003)
summary(afro.grid.year.2)

#######################
#######################
#######################
###Run the IV models###
#######################
#######################
#######################
###Log NL
afro.grid.2$logNL_sum <- log(afro.grid.2$NL_sum+1)
afro.grid.year.2$logNL_sum <- log(afro.grid.year.2$NL_sum+1)
###Create log and square versions of each IV
##Monthly level
afro.grid.2$loglagp.avg <- log(afro.grid.2$lagp.avg+1)
afro.grid.2$lagp.avg2 <- afro.grid.2$lagp.avg^2
afro.grid.2$loglagp.avg2 <- log(afro.grid.2$lagp.avg2+1)
##Annual
afro.grid.year.2$loglagp.avg <- log(afro.grid.year.2$lagp.avg+1)
afro.grid.year.2$lagp.avg2 <- afro.grid.year.2$lagp.avg^2
afro.grid.year.2$loglagp.avg2 <- log(afro.grid.year.2$lagp.avg2+1)

######################
######################
###War and Violence###
######################
######################

#########
###GED###
#########
###State
##Monthly
lm.2.s <- felm(ged_state ~ laglogpopulation+logNL_sum+lagged_state|gid + month + year|
                 (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
               data=afro.grid.2)

summary(lm.2.s)

##Annual
lm.2.s.year <- felm(ged_state ~ laglogpopulation+logNL_sum+lagged_state|gid + year|
                      (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                    data=afro.grid.year.2)

summary(lm.2.s.year)

#####Nonstate
##Monthly
lm.2.ns <- felm(ged_nonstate ~ laglogpopulation+logNL_sum+lagged_nonstate|gid + month + year|
                  (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                data=afro.grid.2)

summary(lm.2.ns)

##Annual
lm.2.ns.year <- felm(ged_nonstate ~ laglogpopulation+logNL_sum+lagged_nonstate|gid + year|
                       (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                     data=afro.grid.year.2)

summary(lm.2.ns.year)

#####Violence against civilians
##Monthly
lm.2.v <- felm(ged_viol_tot ~ laglogpopulation+logNL_sum+lagged_viol_tot|gid + month + year|
                 (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
               data=afro.grid.2)

summary(lm.2.v)
##Annual
lm.2.v.year <- felm(ged_viol_tot ~ laglogpopulation+logNL_sum+lagged_viol_tot|gid + year|
                      (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                    data=afro.grid.year.2)

summary(lm.2.v.year)

##Export to LaTex
#stargazer(lm.2.s,lm.2.ns,lm.2.v,lm.2.s.year,lm.2.ns.year,lm.2.v.year)


###########
###ACLED###
###########
###State
##Monthly
lm.2.s.ac <- felm(acled_battle_state ~ laglogpopulation+logNL_sum+lagacled_battle_state|gid + month + year|
                    (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                  data=afro.grid.2)

summary(lm.2.s.ac)

##Annual
lm.2.s.year.ac <- felm(acled_battle_state ~ laglogpopulation+logNL_sum+lagacled_battle_state|gid + year|
                         (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                       data=afro.grid.year.2)

summary(lm.2.s.year.ac)


#####Nonstate
##Monthly
lm.2.ns.ac <- felm(acled_battle_nonstate ~ laglogpopulation+logNL_sum+lagacled_battle_nonstate|gid + month + year|
                     (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                   data=afro.grid.2)

summary(lm.2.ns.ac)

##Annual
lm.2.ns.year.ac <- felm(acled_battle_nonstate ~ laglogpopulation+logNL_sum+lagacled_battle_nonstate|gid + year|
                          (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                        data=afro.grid.year.2)

summary(lm.2.ns.year.ac)

#####Violence against civilians
##Monthly
lm.2.v.ac <- felm(acled_viol_tot ~ laglogpopulation+logNL_sum+lagacled_viol_tot|gid + month + year|
                    (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                  data=afro.grid.2)

summary(lm.2.v.ac)
##Annual
lm.2.v.year.ac <- felm(acled_viol_tot ~ laglogpopulation+logNL_sum+lagacled_viol_tot|gid + year|
                         (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                       data=afro.grid.year.2)

summary(lm.2.v.year.ac)

###Export to latex
#stargazer(lm.2.s.ac,lm.2.ns.ac,lm.2.v.ac,lm.2.s.year.ac,lm.2.ns.year.ac,lm.2.v.year.ac)

##########
###PITF###
##########
###Monthly
lm.2.s.pitf <- felm(pitf_tot ~ laglogpopulation+logNL_sum+lagpitf_tot|gid + month + year|
                      (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                    data=afro.grid.2)

summary(lm.2.s.pitf)
###Annual
lm.2.v.year.pitf <- felm(pitf_tot ~ laglogpopulation+logNL_sum+lagpitf_tot|gid + year|
                           (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                         data=afro.grid.year.2)

summary(lm.2.v.year.pitf)

##Export to LaTex
#stargazer(lm.2.s.pitf,lm.2.v.year.pitf)

###Export all models to LaTex
stargazer(lm.2.s,lm.2.ns,lm.2.v,lm.2.s.year,lm.2.ns.year,lm.2.v.year,lm.2.s.ac,lm.2.ns.ac,lm.2.v.ac,lm.2.s.year.ac,lm.2.ns.year.ac,lm.2.v.year.ac,lm.2.s.pitf,lm.2.v.year.pitf)

########################
########################
###Protests and Riots###
########################
########################

###########
###ACLED###
###########
###Protests
##Monthly
lm.2.pr.ac <- felm(acled_protests ~ laglogpopulation+logNL_sum+lagacled_protests|gid + month + year|
                     (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                   data=afro.grid.2)

summary(lm.2.pr.ac)

##Annual
lm.2.pr.year.ac <- felm(acled_protests ~ laglogpopulation+logNL_sum+lagacled_protests|gid + year|
                          (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                        data=afro.grid.year.2)

summary(lm.2.pr.year.ac)


#####Riots
##Monthly
lm.2.ri.ac <- felm(acled_riots ~ laglogpopulation+logNL_sum+lagacled_riots|gid + month + year|
                     (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                   data=afro.grid.2)

summary(lm.2.ri.ac)

##Annual
lm.2.ri.year.ac <- felm(acled_riots ~ laglogpopulation+logNL_sum+lagacled_riots|gid + year|
                          (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                        data=afro.grid.year.2)

summary(lm.2.ri.year.ac)


##########
###SCAD###
##########
###Demonstrations
##Monthly
lm.2.pr.scad <- felm(scad_demo ~ laglogpopulation+logNL_sum+lagscad_demo|gid + month + year|
                       (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                     data=afro.grid.2)

summary(lm.2.pr.scad)

##Annual
lm.2.pr.year.scad <- felm(scad_demo ~ laglogpopulation+logNL_sum+lagscad_demo|gid + year|
                            (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                          data=afro.grid.year.2)

summary(lm.2.pr.year.scad)


#####Riots
##Monthly
lm.2.ri.scad <- felm(scad_riot ~ laglogpopulation+logNL_sum+lagscad_riot|gid + month + year|
                       (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                     data=afro.grid.2)

summary(lm.2.ri.scad)

##Annual
lm.2.ri.year.scad <- felm(scad_riot ~ laglogpopulation+logNL_sum+lagscad_riot|gid + year|
                            (lagNDVI_mean~loglagp.avg+loglagp.avg2)|gid,
                          data=afro.grid.year.2)

summary(lm.2.ri.year.scad)

##Export to LaTex
stargazer(lm.2.pr.ac,lm.2.ri.ac,lm.2.pr.year.ac,lm.2.ri.year.ac,lm.2.pr.scad,lm.2.ri.scad,lm.2.pr.year.scad,lm.2.ri.year.scad)





